﻿using Ldt.SettingCenter.Core;
using LdtCommonLibraryHttp;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.CallInterface.ConfigManager;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Ldt.WindowsFormsApp.ToolsForm
{
    
    public partial class FormSeetingCenter : UIForm
    {
        public string settingcenterstr = string.Empty;
        public event SetConfigRetrievalHandlear setConfigEvent;
        public FormSeetingCenter(IConfiguration configuration)
        {
            InitializeComponent();
            _configuration = configuration;
        }

        private IConfiguration _configuration;
        private void temporaryButton_Click(object sender, EventArgs e)
        {
            _configuration["appsettings:setting_center_address"] = settingcenterTextBox.Text;
            settingcenterstr = settingcenterTextBox.Text;
            if (!string.IsNullOrWhiteSpace(settingcenterTextBox.Text))
                setConfigEvent(settingcenterTextBox.Text);
            MessageBox.Show("数据配置应用完成！");
            this.Close();
        }
        private void permanentButton_Click(object sender, EventArgs e)
        {
            string jsonString = File.ReadAllText(System.IO.Path.Combine(System.AppContext.BaseDirectory, "appsettings.json"),Encoding.GetEncoding("GB2312"));//读取文件
            JObject jobject = JObject.Parse(jsonString);//解析成json
            jobject["appSettings"]["setting_center_address"] = settingcenterTextBox.Text;//替换需要的文件
            string convertString = Convert.ToString(jobject);//将json装换为string
            File.WriteAllText(System.IO.Path.Combine(System.AppContext.BaseDirectory, "appsettings.json"), convertString, Encoding.UTF8);//将内容写进jon文件中
            MessageBox.Show("数据配置应用完成，已经存储到文件！");
            this.Close();
        }

        private void FormSeetingCenter_Load(object sender, EventArgs e)
        {
            settingcenterTextBox.Text = _configuration["appsettings:setting_center_address"];
        }
    }
}
